<?php

class ContentsController extends AppController {
  //public $scaffold;
  public $helpers = array('Html', 'TWSForm');
  public $components = array('Session', 'Util');
  public $name = 'Contents';
  public $map = array();
  
  function __construct($request=null, $response=null) {
    parent::__construct($request, $response);
    $this->map= Configure::read('App.contentPage');
  }

  function message(){
    $this->layout='main';
  }

  function filter($keyVal=null) {
    if (empty($keyVal)) {
      $this->home();
    } else {
      switch ($keyVal) {
        case $this->map['home'] :
          $this->home();
          break;
        case $this->map['breeders'] :
          $this->breeders();
          break;
        case $this->map['classes'] :
          $this->classes();
          break;
        case $this->map['contactus'] :
          $this->contact_us();
          break;
        case $this->map['events'] :
          $this->contact_us();
          break;
        case $this->map['ourclub'] :
          $this->our_club();
          break;
        case $this->map['ourshow'] :
          $this->our_show();
          break;
        default :
          $this->home();
          break;

      }
    }
  }

  function member_home() {
    $this->set('title_for_layout','Champaign Illinois Kennel Club Member Home Page');
  }

  function admin_home() {
    $this->layout = 'admin';
    $this->set('title_for_layout','Champaign Illinois Kennel Club Administrative Home Page');
  }

  function admin_restore($contentId) {
    $this->loadModel('ContentAudit');
    if(empty($contentId)) {
      throw new NotFoundException();
    } else {
        $record = $this->ContentAudit->find("first", array("conditions"=>array('id'=>$contentId)));
        if(empty($record)){
          throw new NotFoundException();
        } else {
          $this->loadModel('Content');
          $data = array("Content"=>array("id"=>$record['ContentAudit']['content_id'], "info"=>$record['ContentAudit']['info']));
          $result= $this->Content->save($data, false, array('info'));
          if(!empty($result)) {
            $this->Session->setFlash('The page content was successfully restored.');
          } else {
            $this->Session->setFlash('An error occurred and the page content was not successfully restored.');
          }
          $this->redirect(array("action"=>"index", "admin"=>true));
        }
    }
  }

  function admin_view($contentId) {
      $this->layout = 'admin';
      $this->loadModel('ContentAudit');
      $this->loadModel('Content');
      if (empty($contentId)) {
        throw new NotFoundException();
      } else {
        $pageInfo = $this->Content->find('first',array('conditions'=>array('Content.id'=>$contentId)));
        if(empty($pageInfo)){
          throw new NotFoundException();
        } else {
          $this->set('pageInfo', $pageInfo);
          $records = $this->ContentAudit->find("all", array("conditions" => array('content_id' => $contentId), "order" => array("ContentAudit.modified DESC", "ContentAudit.created DESC"), "limit" => Configure::read('App.contentRestoreLimit')));
          $this->set("records", $records);
        }
      }
  }

  function preview($auditId) {
    $this->layout='main';
    if(empty($auditId)) {
      throw new NotFoundException();
    } else {
      $this->loadModel('ContentAudit');
      $content = $this->ContentAudit->read(null, $auditId);
      if (empty($content)){
        throw new NotFoundException();        
      } else {
        $this->set('content',$content);
      }
    }
  }

  function home() {
    $this->loadModel('HomepagePhoto');
    $this->set("title_for_layout","Champaign Illinois Kennel Club - All-breed AKC member club located in East Central Illinois.");
    $this->set("meta_keywords","Champaign Illinois Kennel Club, AKC, Dog club, Pure Bred Dogs, Central Illinois, Champaign");
    $this->set("meta_description","We are an all-breed AKC member club located in East Central Illinois.  Our membership is dedicated to promoting purebred dogs and activities throughout the community.");
    $this->Content->id = $this->map['home'];
    $result = $this->Content->read();
    $photos = $this->HomepagePhoto->find("all", array("order" => array("HomepagePhoto.created" => 'desc')));
    $this->set('contents', $result);
    $this->set('photos', $photos);
    $this->render('home', 'main');
  }

  function our_club() {
    $this->set("title_for_layout","About the Champaign Illinois Kennel Club");
    $this->set("meta_keywords","About History Officers Champaign Illinois Kennel Club, CIKC, AKC, Dog club, Pure Bred Dogs, Central Illinois, Champaign");
    $this->set("meta_description","About the history of the Champaign Illinois Kennel Club. Club Officers and other important information about the club are listed here.");
    $this->Content->id = $this->map['ourclub'];
    $result = $this->Content->read();
    $this->set('contents', $result);
    $this->render('our_club', 'main');
  }

   function breeders() {
     $this->set("title_for_layout","Breeders Information for the Champaign Illinois Kennel Club");
    $this->set("meta_keywords","Dog Breeder Information Champaign Illinois Kennel Club, CIKC, AKC, Dog club, Pure Bred Dogs, Central Illinois, Champaign");
    $this->set("meta_description","Information about Breeders and information for responsible dog owners are provided here.");
    $this->Content->id = $this->map['breeders'];
    $result = $this->Content->read();
    $this->set('contents', $result);
    $this->render('breeders', 'main');
  }

   function classes() {
     $this->set("title_for_layout","Classes offered by the Champaign Illinois Kennel Club.");
    $this->set("meta_keywords","Classes Conformation Obedience Rally Champaign Illinois Kennel Club, CIKC, AKC, Dog club, Pure Bred Dogs, Central Illinois, Champaign");
    $this->set("meta_description","The Champaign Illinois Kennel Club hosts a variety of classes for dog owners from conformation to obedience to puppy training classes.");
    $this->Content->id = $this->map['classes'];
    $result = $this->Content->read();
    $this->set('contents', $result);
    $this->render('classes', 'main');
  }


   function our_show() {
     $this->set("title_for_layout","Champaign Illinois Kennel Club Dog Show");
    $this->set("meta_keywords","Dog Show Annual Show Event Champaign Illinois Kennel Club, CIKC AKC, Dog club, Pure Bred Dogs, Central Illinois, Champaign");
    $this->set("meta_description","Champaign Illinois Kennel Club hosts a dog show every year. This page provides important information on our show.");
    $this->Content->id = $this->map['ourshow'];
    $result = $this->Content->read();
    $this->set('contents', $result);
    $this->render('our_show', 'main');
  }

   function events() {
     $this->set("title_for_layout","Events held by the Champaign Illinois Kennel Club");
    $this->set("meta_keywords","Events Champaign Illinois Kennel Club, CIKC, AKC, Dog club, Pure Bred Dogs, Central Illinois, Champaign");
    $this->set("meta_description","The Champaign Illinois Kennel Club hosts events every year catered towards dogs and dog owners. Here is a list of events that are coming up.");
    $this->Content->id = $this->map['events'];
    $result = $this->Content->read();
    $this->set('contents', $result);
    $this->render('events', 'main');
  }

//admin and public version
  function member_index() {
    //Display a list of content pages
    $this->set('options', $this->Content->find('list', array('fields'=> array('Content.id','Content.content_description'), 'order'=>array('Content.content_description'=>'asc')  )));
  }

  function admin_index() {
    //Display a list of content pages
    $this->layout='admin';
    $this->set("title_for_layout","Restore Content Pages");
    $this->set('options', $this->Content->find('list', array('fields'=> array('Content.id','Content.content_description'), 'order'=>array('Content.content_description'=>'asc')  )));
  }


  //admin

  function member_view() {    
      $id = $this->request->data["id"];
      if (!empty($id)) {
         $this->redirect(array('action' => 'edit', 'member'=>true, $id));
      }

      //$this->render('member_edit');
    
  }


  function member_edit($id = null) {
    $this->Content->id = $id;
    if ($this->request->is('get')) {
      $this->set('contents', $this->Content->read());
    } else {
        $this->_processAction($this->request, 'edit', $id);
      }    
  }

  private function _processAction($dataIn, $action='add', $id=null) {
      $data = $this->_processRequestData($dataIn);
      $data['Content']['id']=$id;
      $this->Content->set($data);
      if ($this->Content->validates()) {
        if ($this->Content->save($data, false)) {
          $this->Session->setFlash('The record was successfully updated.');
          $this->redirect(array('action' => 'index'));
        }
      } 
  }


}

?>
